package com.budwk.app.sqfwDoc.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;
import java.util.Properties;

/**
 * @author ZY.Wang
 * @since 2023/12/18
 */

public class SqfwCodeGeneratorUtils {

    public static void main(String[] args) {

        SqfwCodeGeneratorUtils.initVelocity();
        VelocityContext velocityContext = SqfwCodeGeneratorUtils.prepareContext();

        String template = "templet/sqfwGenCode/controller.java.vm";
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, "UTF-8");
        tpl.merge(velocityContext, sw);

        String s = sw.toString();
        System.out.println(s);
    }

    /**
     * 初始化vm方法
     */
    public static void initVelocity() {
        Properties p = new Properties();
        try {
            // 加载classpath目录下的vm文件
            p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            // 定义字符集
            p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
            // 初始化Velocity引擎，指定配置Properties
            Velocity.init(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 设置模板变量信息
     *
     * @return 模板列表
     */
    public static VelocityContext prepareContext() {

        VelocityContext velocityContext = new VelocityContext();
        return velocityContext;
    }

    public static void setMenuVelocityContext(VelocityContext context) {
        //String options = genTable.getOptions();
        //JSONObject paramsObj = JSON.parseObject(options);
        //String parentMenuId = getParentMenuId(paramsObj);
        //context.put("parentMenuId", parentMenuId);
    }
}
